package com.example.yuan;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Objects;


public class MainActivity extends AppCompatActivity {

    private AppCompatImageView backImageView = null;
    private AppCompatImageView goImageView = null;
    private AppCompatImageView refreshImageView = null;
    private AppCompatEditText linkEditText = null;
    private AppCompatImageView sendImageView = null;
    private AppCompatImageView favImageView = null;
    private SwipeRefreshLayout maniSwipeRefreshLayout = null; //下拉刷新
    private WebView mainWebView = null;
    private ContentLoadingProgressBar mainProgressBar = null;//进度条

    private ArrayList<TagsData> tagsArrayList = new ArrayList<>();


    private Handler handler = new Handler() {  //ctrl+o,选择第一个
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                maniSwipeRefreshLayout.setRefreshing(false);
                mainWebView.reload();
            }
        }
    };


   /* private AppCompatEditText mobileEditText = null;
    private AppCompatTextView nexTextView = null;//实例化

    private AppCompatRadioButton boyRadioButton = null;
    private AppCompatRadioButton girlRadioButton = null;//实例化

    private AppCompatTextView addTextView = null;
    private AppCompatTextView subTextView = null;
    private ContentLoadingProgressBar oneProgressBar = null;*/

   /* private ProgressDialog progressDialog = null;
    private AppCompatTextView progressTextView = null;

    private AppCompatTextView alertTextView = null;*/


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_llq);

        backImageView = findViewById(R.id.backImageView);
        goImageView = findViewById(R.id.goImageView);
        refreshImageView = findViewById(R.id.refreshImageView);
        linkEditText = findViewById(R.id.linkEditText);
        sendImageView = findViewById(R.id.sendImageView);
        maniSwipeRefreshLayout = findViewById(R.id.maniSwipeRefreshLayout);
        favImageView = findViewById(R.id.favImageView);
        mainWebView = findViewById(R.id.mainWebView);
        mainProgressBar = findViewById(R.id.mainProgressBar);


        maniSwipeRefreshLayout.setColorSchemeColors(
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark),
                getResources().getColor(R.color.colorPrimary)
        );

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mainWebView.loadUrl(url);
                return true;
            }
        });


        mainWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mainProgressBar.setVisibility(View.VISIBLE);
                mainProgressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    mainProgressBar.setVisibility(View.GONE);
                }

            }
        });

        backImageView.setOnClickListener(v -> {
            mainWebView.goBack();//后退
        });

        goImageView.setOnClickListener(v -> {
            mainWebView.goForward();//前进
        });

        refreshImageView.setOnClickListener(v -> {
            mainWebView.reload();//重新加载
        });

        favImageView.setOnClickListener(v -> {
            //去重
            TagsData data;
            for (int i = 0; i < tagsArrayList.size(); i++) {
                data = tagsArrayList.get(i);
                if (data.getTitle().equals(mainWebView.getTitle()) || data.getUrl().equals(mainWebView.getUrl())) {
                    Toast.makeText(MainActivity.this, "此书签已存在~", Toast.LENGTH_LONG).show();
                    return;
                }
            }

            //到这里代表整个数据没有重复
            data = new TagsData();
            data.setTitle(mainWebView.getTitle());
            data.setUrl(mainWebView.getUrl());
            tagsArrayList.add(data);
            Toast.makeText(MainActivity.this, "已加入书签~", Toast.LENGTH_LONG).show();
        });

        sendImageView.setOnClickListener(v -> {
            try {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse("http:://www.cnblogs.com");
                intent.setData(content_url);
                startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, "未安装对应的应用", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        });

        favImageView.setOnLongClickListener(v -> {
            startActivity(new Intent(MainActivity.this, TagsActivity.class));
            return false;
        });


        sendImageView.setOnClickListener(v ->
                mainWebView.loadUrl(Objects.requireNonNull(linkEditText.getText()).toString()));


        maniSwipeRefreshLayout.setOnRefreshListener(() ->
                new Handler().postDelayed(() -> {

                    maniSwipeRefreshLayout.setRefreshing(false);
                    mainWebView.reload();
                }, 3000)
        );
    }

    public ArrayList<TagsData> getTagsArrayList() {
        return tagsArrayList;
    }

    public void setTagsArrayList(ArrayList<TagsData> tagsArrayList) {
        this.tagsArrayList = tagsArrayList;
    }

    private class TagsActivity {
    }
}



      /*  progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.incrementProgressBy(20);

        //设置点击外部不消失
        progressDialog.setCanceledOnTouchOutside(true);
        //设置对话框标题
        progressDialog.setTitle("正在加载~");

        progressTextView = findViewById(R.id.progressTextView);
        progressTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.show();
            }
        });

        alertTextView = findViewById(R.id.alertTextView);
        alertTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("确认您的选择")
                        .setMessage("删除这条数据？");
            }
        });


       /* addTextView = findViewById(R.id.addTextView);
        subTextView = findViewById(R.id.subTextView);
        oneProgressBar = findViewById(R.id.oneProgressBar);

        addTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oneProgressBar.setProgress(oneProgressBar.getProgress() + 10);
            }
        });

        subTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oneProgressBar.setProgress(oneProgressBar.getProgress() - 10);
            }
        });

        /*boyRadioButton = findViewById(R.id.boyRadioButton);
        girlRadioButton = findViewById(R.id.girlRadioButton);
        nexTextView = findViewById(R.id.nextTextView);
        nexTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        if (boyRadioButton.isChecked()) {
                                Toast.makeText(MainActivity.this,"boy check",Toast.LENGTH_SHORT).show();
                                return;
                        }
                }
        });



       /* mobileEditText = findViewById(R.id.mobileEditText);
        nexTextView = findViewById(R.id.nextTextView);
        //提醒
        nexTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        String mobile = Objects.requireNonNull(mobileEditText.getText()).toString();
                        Toast.makeText( MainActivity.this,"吐司" ,Toast.LENGTH_SHORT).show();
                }
        });
//水波纹
        /*nexTextView = findViewById(R.id.nextTextView);

        /*nexTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        nexTextView.setText("确定");
                }
        });*/
        /*nexTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
        });*/
